package pw.orm.db;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Logger;

/**
 * @author Eric
 */
public class DefaultDataSource implements DataSource {
  private ClassLoader driverClassLoader;
  private Properties driverProperties;
  private boolean driverInitialized;
  private String driver;
  private String url;
  private String username;
  private String password;
  private boolean autoCommit =true;
  private Integer defaultTransactionIsolationLevel;

  public DefaultDataSource() {
  }

  public DefaultDataSource(String driver, String url, String username, String password) {
    this.driver = driver;
    this.url = url;
    this.username = username;
    this.password = password;
  }

  public DefaultDataSource(String driver, String url, Properties driverProperties) {
    this.driver = driver;
    this.url = url;
    this.driverProperties = driverProperties;
  }

  public DefaultDataSource(ClassLoader driverClassLoader, String driver, String url, String username, String password) {
    this.driverClassLoader = driverClassLoader;
    this.driver = driver;
    this.url = url;
    this.username = username;
    this.password = password;
  }

  public DefaultDataSource(ClassLoader driverClassLoader, String driver, String url, Properties driverProperties) {
    this.driverClassLoader = driverClassLoader;
    this.driver = driver;
    this.url = url;
    this.driverProperties = driverProperties;
  }

  public Connection getConnection() throws SQLException {
    this.initializeDriver();
    Connection connection;
    if (this.driverProperties != null) {
      connection = DriverManager.getConnection(this.url, this.driverProperties);
    } else if (this.username == null && this.password == null) {
      connection = DriverManager.getConnection(this.url);
    } else {
      connection = DriverManager.getConnection(this.url, this.username, this.password);
    }

    this.configureConnection(connection);
    return connection;
  }

  public Connection getConnection(String username, String password) throws SQLException {
    this.initializeDriver();
    Connection connection = DriverManager.getConnection(this.url, username, password);
    this.configureConnection(connection);
    return connection;
  }

  public void setLoginTimeout(int loginTimeout) throws SQLException {
    DriverManager.setLoginTimeout(loginTimeout);
  }

  public int getLoginTimeout() throws SQLException {
    return DriverManager.getLoginTimeout();
  }

  public void setLogWriter(PrintWriter logWriter) throws SQLException {
    DriverManager.setLogWriter(logWriter);
  }

  public PrintWriter getLogWriter() throws SQLException {
    return DriverManager.getLogWriter();
  }

  public ClassLoader getDriverClassLoader() {
    return this.driverClassLoader;
  }

  public void setDriverClassLoader(ClassLoader driverClassLoader) {
    this.driverClassLoader = driverClassLoader;
  }

  public Properties getDriverProperties() {
    return this.driverProperties;
  }

  public void setDriverProperties(Properties driverProperties) {
    this.driverProperties = driverProperties;
  }

  public String getDriver() {
    return this.driver;
  }

  public synchronized void setDriver(String driver) {
    this.driver = driver;
    this.driverInitialized = false;
  }

  public String getUrl() {
    return this.url;
  }

  public void setUrl(String url) {
    this.url = url;
  }

  public String getUsername() {
    return this.username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getPassword() {
    return this.password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public boolean isAutoCommit() {
    return this.autoCommit;
  }

  public void setAutoCommit(boolean autoCommit) {
    this.autoCommit = autoCommit;
  }

  public Integer getDefaultTransactionIsolationLevel() {
    return this.defaultTransactionIsolationLevel;
  }

  public void setDefaultTransactionIsolationLevel(Integer defaultTransactionIsolationLevel) {
    this.defaultTransactionIsolationLevel = defaultTransactionIsolationLevel;
  }

  private void configureConnection(Connection conn) throws SQLException {
    if (this.autoCommit != conn.getAutoCommit()) {
      conn.setAutoCommit(this.autoCommit);
    }

    if (this.defaultTransactionIsolationLevel != null) {
      conn.setTransactionIsolation(this.defaultTransactionIsolationLevel.intValue());
    }

  }

  private synchronized void initializeDriver() {
    if (!this.driverInitialized) {
      this.driverInitialized = true;

      try {
        Class driverType;
        if (this.driverClassLoader != null) {
          driverType = Class.forName(this.driver, true, this.driverClassLoader);
        } else {
          driverType = Class.forName(this.driver, true,
                  Thread.currentThread().getContextClassLoader());
        }

        DriverManager.registerDriver((Driver) driverType.newInstance());
      } catch (Exception var3) {
        throw new RuntimeException("Error setting driver on DataSource. Cause: "+var3.getLocalizedMessage());
      }
    }

  }

  public <T> T unwrap(Class<T> iface) throws SQLException {
    throw new UnsupportedOperationException();
  }

  public boolean isWrapperFor(Class<?> iface) throws SQLException {
    throw new UnsupportedOperationException();
  }

  public Logger getParentLogger()  {
    throw new UnsupportedOperationException();
  }
}
